package com.ostrich.a2104gao5

import kotlin.math.roundToInt

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/14 10:45
 * @Description : Test8
 */
fun main(){
    /*******字符串操作********/
    var NAME = "yaotianxue"
    var NAMES = "yaotianxue,zhangsan,lisi"
    //字符串截取
    val index = NAME.indexOf("o") //获得o的下标
    val str = NAME.substring(0 until index ) //截取 从0到o的下标
    println(str)
    //切割
    val data = NAMES.split(",")
    println(data[0])
    //切割解构语法
    val (n1,n2,n3) =  NAMES.split(",")
    println("$n1 $n2 $n3")
    //替换
    val strr = "xiaoming,danni,xiaohong!"
    val str2 = strr.replace(Regex("[xio]")){
        when(it.value){
            "x"-> "1"
            "i"-> "2"
            "o"-> "3"
            else->it.value
        }
    }
    println(str2)
    //java:==和equals的区别：==比较地址 equals比较内容
    //kotlin：==和===的区别：==判断内容 ===判断地址
    val str3 = "Jason"
    val str4 = "Jason"
    println(str3 == str4)//true
    println(str3 === str4)//true
    val str33 = "Jason"
    val str44 = "jason".capitalize()
    println(str33 == str44)//true
    println(str33 === str44)//false
    //遍历
    strr.forEach {
        println("$it")
    }
    /*****数字类型安全转换******/
//    val number:Int = "8.98".toInt()
    val number:Int? = "8.98".toIntOrNull()
    println(number)
    /*****Double转Int数字格式化******/
    println(8.985678.toInt())
    println(8.985678.roundToInt())//四舍五入
    val s:String = "%.3f".format(8.985678)//保留2位小树
    println(s)

}